import prisma from '@/server/lib/prisma';
import dayjs from 'dayjs';

export default defineEventHandler(async (event) => {
  try {
    const fmt = 'YYYY-MM-DD';
    const now = dayjs();
    const startOfWeek = now.startOf('week');
    const weekRange = [];
    for (let i = 0; i < 7; i++) {
      weekRange.push(startOfWeek.add(i, 'day').format(fmt));
    }
    const results = [];
    for (const date of weekRange) {
      const count = await prisma.sys_user.count({
        where: {
          createTime: {
            gte: new Date(date + ' 00:00:00').toISOString(),
            lte: new Date(date + ' 23:59:59').toISOString(),
          },
        },
      })
      results.push({
        date,
        count,
      })
    }

    return {
      code: 200,
      data: results,
    }
  } catch (e) {
    throw e;
  } finally {
    await prisma.$disconnect(); // 关闭连接
  }
})